package fr.miage.resources;

import java.io.IOException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import fr.miage.dao.InvitationDAO;
import fr.miage.model.Invitation;
import fr.miage.model.User;

@Path("/invitation")
public class InvitationResource {
	private InvitationDAO invitDAO = new InvitationDAO();
	private Logger logger = Logger.getRootLogger();
	
	@GET
	@Path("/{idEvent}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getInvitation(@QueryParam(value="idUser") String idUser, @PathParam(value="idEvent") String idEvent){
		Invitation invit = invitDAO.getById(idUser, idEvent);
		ObjectMapper mapper = new ObjectMapper();
		if(invit == null){
			return Response.status(404).build();
		}else{
			String fluxJson;
			try {
				fluxJson = mapper.writeValueAsString(invit);
				return Response.ok(fluxJson).build();
			} catch (JsonGenerationException e) {
				logger.error(e);
				return Response.status(500).build();
			} catch (JsonMappingException e) {
				logger.error(e);
				return Response.status(500).build();
			} catch (IOException e) {
				logger.error(e);
				return Response.status(500).build();
			}
		}
	}
	
	@GET
	public Response getAllInvitationByUser(@QueryParam(value="idUser")String idUser){
		List<Invitation> listeInvit = invitDAO.getAllInvitByUser(idUser);
		ObjectMapper mapper = new ObjectMapper();
		if(listeInvit == null){
			return Response.status(404).build();
		}else if(listeInvit.size() == 0){
			return Response.status(204).build();
		}else{
			String fluxJson;
			try {
				fluxJson = mapper.writeValueAsString(listeInvit);
				return Response.ok(fluxJson).build();
			} catch (JsonGenerationException e) {
				logger.error(e);
				return Response.status(500).build();
			} catch (JsonMappingException e) {
				logger.error(e);
				return Response.status(500).build();
			} catch (IOException e) {
				logger.error(e);
				return Response.status(500).build();
			}
		}
	}
	
	
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	public Response addInvitation(Invitation invit){
		if (invit == null) {
			return Response.status(400).entity("Invitation n'est pas correctement formé").build();
		}else{
			if(!invit.getRole().matches("[a-zA-Z]+")){
				return Response.status(400).entity("L'intitule contient des caractères non autorisés").build();
			}
			if(!invit.getResponse().matches("[0-9]+")){
				return Response.status(400).entity("Le type event contient des caractères non autorisés").build();
			}
			int nbRes = invitDAO.addInvitation(invit);
			if(nbRes != 1){
				return Response.status(400).entity("Les paramètres entrés sont incorrects (clés étrangères non respectées ou event existe déjà)").build();
			}else{
				return Response.ok().build();
			}
		}
	}
	/*

	@GET
	public Response getAllUserByEvent(@QueryParam(value="idEvent")String idEvent){
		List<User> listeUser = userDAO.getAllUserByEvent(idEvent);
		ObjectMapper mapper = new ObjectMapper();
		if(listeUser == null || listeUser.size() == 0){
			return Response.status(404).build();
		}else{
			String fluxJson;
			try {
				fluxJson = mapper.writeValueAsString(listeUser);
				return Response.ok(fluxJson).build();
			} catch (JsonGenerationException e) {
				logger.error(e);
				return Response.status(500).build();
			} catch (JsonMappingException e) {
				logger.error(e);
				return Response.status(500).build();
			} catch (IOException e) {
				logger.error(e);
				return Response.status(500).build();
			}
		}
	}*/
}
